package me.yiyou.meihua.ui.page.sixlines

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import me.yiyou.meihua.R
import me.yiyou.meihua.element.CustomButton
import me.yiyou.meihua.ui.screen.Screen

@Composable
fun SixLinesPage(viewModel: SixLineViewModel = viewModel(), navController: NavController) {

    val lineArray by viewModel.lineArray

    Column(
        modifier = Modifier
            .padding(top = 60.dp)
            .fillMaxWidth()
            .fillMaxHeight(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                painter = painterResource(R.drawable.icon_yingbizhengmian),
                modifier = Modifier
                    .size(30.dp)
                    .align(Alignment.CenterVertically),
                contentDescription = null
            )
            Text("正面", fontSize = 10.sp)
            Image(
                painter = painterResource(R.drawable.icon_yingbifanmian),
                modifier = Modifier
                    .size(30.dp)
                    .align(Alignment.CenterVertically),
                contentDescription = null
            )
            Text("反面", fontSize = 10.sp)
        }
        CustomButton(
            text = "开始起卦",
            onClick = { viewModel.createRandomLineArray() },
            modifier = Modifier
                .width(140.dp)
                .padding(vertical = 10.dp)
                .fillMaxWidth()
        )
        getSixLines(lineArray, 0)
        getSixLines(lineArray, 1)
        getSixLines(lineArray, 2)
        getSixLines(lineArray, 3)
        getSixLines(lineArray, 4)
        getSixLines(lineArray, 5)
        if (viewModel.isGenerate.value) {
            CustomButton(
                text = "装卦",
                onClick = { navController.navigate(Screen.SixLinesCreate.route) },
                modifier = Modifier
                    .width(140.dp)
                    .padding(vertical = 10.dp)
                    .fillMaxWidth()
            )
        }
    }
}

@Composable
fun getSixLines(lineArray: List<List<Int>>, i: Int) {
    if (lineArray.size == 0) {
        return
    }
    Row(
        modifier = Modifier.padding(16.dp),
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
    ) {
        val resId1 =
            if (lineArray[i][0] == 1) R.drawable.icon_yingbizhengmian else R.drawable.icon_yingbifanmian
        val resId2 =
            if (lineArray[i][1] == 1) R.drawable.icon_yingbizhengmian else R.drawable.icon_yingbifanmian
        val resId3 =
            if (lineArray[i][2] == 1) R.drawable.icon_yingbizhengmian else R.drawable.icon_yingbifanmian
        Image(
            painter = painterResource(resId1),
            modifier = Modifier
                .size(40.dp)
                .align(Alignment.CenterVertically),
            contentDescription = null
        )
        Image(
            painter = painterResource(resId2),
            modifier = Modifier
                .size(40.dp)
                .align(Alignment.CenterVertically),
            contentDescription = null
        )
        Image(
            painter = painterResource(resId3),
            modifier = Modifier
                .size(40.dp)
                .align(Alignment.CenterVertically),
            contentDescription = null
        )
        if (lineArray[i][0] == 1 && lineArray[i][1] == 1 && lineArray[i][2] == 1) {
            Text("○", fontSize = 10.sp)
        } else if (lineArray[i][0] == 0 && lineArray[i][1] == 0 && lineArray[i][2] == 0) {
            Text("X", fontSize = 10.sp)
        } else {
            Text("○", fontSize = 10.sp, modifier = Modifier.alpha(0f))
        }
    }
}

